package com.cn.transfer.jobhandler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.entity.jiandaoyun.Mx;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.PurchaseOrder;
import com.cn.transfer.common.vo.PurchaseOrderDetail;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.PuPurchaseOrderBMapper;
import com.cn.transfer.mapper.PuPurchaseOrderMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author: 向伟典
 * @date: 2023/9/6 10:23
 * @Description: 处理采购订单修改的内容到简道云
 */
@Slf4j
@Component
public class PurchaseUpdateXxlJob {

    @Autowired
    private PuPurchaseOrderMapper puPurchaseOrderMapper;
    @Autowired
    private PuPurchaseOrderBMapper puPurchaseOrderBMapper;

    @Transactional
    @XxlJob("purchaseOrder")
    public void purchaseOrder() {
        List<PurchaseOrder> purchaseOrders = puPurchaseOrderMapper. byChangedateList();  // 主表
        int i = 0;
        for (PurchaseOrder purchaseOrder1 : purchaseOrders) {
            if (puPurchaseOrderMapper.inquireIsExist(purchaseOrder1.getCode())==0){
                continue;
            }
            PurchaseOrder purchaseOrder = puPurchaseOrderMapper.byvvPurchaseOrderCode(String.valueOf(purchaseOrder1.getPurchaseOrderId()));   // 表单
            Map<String, Object> stringObjectMap = arePurchaseOrdersEqual(purchaseOrder, purchaseOrder1);
            boolean code = (boolean) stringObjectMap.get("code");
            if (!code) {
                int str = (int) stringObjectMap.get("data");
                if (puPurchaseOrderMapper.inquireIsExist(purchaseOrder1.getCode())>0){
                    // 不一致同步到视图和简道云
                    puPurchaseOrderMapper.deletePurchaseOrderId(String.valueOf(str));
                    puPurchaseOrderBMapper.deleteIdPurchaseOrderDTO(String.valueOf(str));
                }
                Header[] headers = ConnmeUtils.createHeaders();
                Map<String, Object> parameterDelete = new HashMap<>();
                parameterDelete.put("app_id", "64f00ef131adb70007ad6e6d");
                parameterDelete.put("entry_id", "64f00f0e68761f0007719cf9");
                List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameterDelete, headers, purchaseOrder.getCode(),"djbh");
                if (jdyPurchaseOrders.size() > 0) {
                    puPurchaseOrderMapper.insertVVPuechase(purchaseOrder1);
                    for (PurchaseOrderDetail purchaseOrderDetail : purchaseOrder1.getPurchaseOrderDetails()) {
                        puPurchaseOrderBMapper.insertPurchaseOrderDetail(purchaseOrderDetail);
                    }
                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "64f00ef131adb70007ad6e6d");
                    parameterInster.put("entry_id", "64f00f0e68761f0007719cf9");
                    parameterInster.put("data_id", jdyPurchaseOrders.get(0).get_id());
                    Map<String, Object> data = new HashMap<>();

                    Map<String, Object> ghs = new HashMap<>();
                    ghs.put("value", purchaseOrder1.getPartnerName());
                    data.put("ghs", ghs);

                    Map<String, Object> djbh = new HashMap<>();
                    djbh.put("value", purchaseOrder1.getCode());
                    data.put("djbh", djbh);

                    Map<String, Object> yjdh = new HashMap<>();
                    yjdh.put("value", purchaseOrder1.getAcceptDate());
                    data.put("yjdh", yjdh);

                    Map<String, Object> bm = new HashMap<>();
                    bm.put("value", purchaseOrder1.getDepartmentName());
                    data.put("bm", bm);

                    Map<String, Object> ghsbm = new HashMap<>();
                    ghsbm.put("value", purchaseOrder1.getPartnerCode());
                    data.put("ghsbm", ghsbm);

                    Map<String, Object> ghsjc = new HashMap<>();
                    ghsjc.put("value", purchaseOrder1.getPartnerAbbName());
                    data.put("ghsjc", ghsjc);

                    Map<String, Object> cgy = new HashMap<>();
                    cgy.put("value", purchaseOrder1.getPurchaserAddress());
                    data.put("cgy", cgy);

                    Map<String, Object> ck = new HashMap<>();
                    ck.put("value", purchaseOrder1.getName());
                    data.put("ck", ck);

                    Map<String, Object> ddje = new HashMap<>();
                    ddje.put("value", purchaseOrder1.getOrigEarnestMoney());
                    data.put("ddje", ddje);

                    Map<String, Object> djrq = new HashMap<>();
                    djrq.put("value", purchaseOrder1.getVoucherdate());
                    data.put("djrq", djrq);

                    Map<String, Object> xsddh = new HashMap<>();
                    xsddh.put("value", purchaseOrder1.getSaleOrderCode());
                    data.put("xsddh", xsddh);

                    Map<String, Object> chdd = new HashMap<>();
                    chdd.put("value", purchaseOrder1.getPubuserdefnvc5());
                    data.put("chdd", chdd);

                    Map<String, Object> ywbm = new HashMap<>();
                    ywbm.put("value", purchaseOrder1.getPubuserdefnvc2());
                    data.put("ywbm", ywbm);
                    Map<String, Object> zdr = new HashMap<>();
                    zdr.put("value", purchaseOrder1.getMakerAddress());
                    data.put("zdr", zdr);

                    Map<String, Object> zdsj = new HashMap<>();
                    zdsj.put("value", purchaseOrder1.getCreateDateTime());
                    data.put("zdsj", zdsj);

                    Map<String, Object> shsj = new HashMap<>();
                    shsj.put("value", purchaseOrder1.getAuditDateTime());
                    data.put("shsj", shsj);

                    Map<String, Object> xsddzdr = new HashMap<>();
                    xsddzdr.put("value", purchaseOrder1.getSalesPersonAddress());
                    data.put("xsddzdr", xsddzdr);

                    Map<String, Object> xsddywy = new HashMap<>();
                    xsddywy.put("value", purchaseOrder1.getSalesMakerAddress());
                    data.put("xsddywy", xsddywy);

                    Map<String, Object> yjdhsj = new HashMap<>();
                    yjdhsj.put("value", purchaseOrder1.getEstimatedDateTime());
                    data.put("yjdhsj", yjdhsj);

                    List<Map<String, Object>> result = new ArrayList<>();
                    for (PurchaseOrderDetail detail : purchaseOrder1.getPurchaseOrderDetails()) {
                        Map<String, Object> detailMap = new HashMap<>();

                        Map<String, Object> inventoryNameMap = new HashMap<>();
                        inventoryNameMap.put("value", detail.getInventoryName());
                        detailMap.put("mx_chmc", inventoryNameMap);

                        Map<String, Object> productCodeMap = new HashMap<>();
                        productCodeMap.put("value", detail.getProductCode());
                        detailMap.put("mx_cpbm", productCodeMap);

                        Map<String, Object> productNameNewMap = new HashMap<>();
                        productNameNewMap.put("value", detail.getProductNameNew());
                        detailMap.put("mx_cpmcx", productNameNewMap);

                        Map<String, Object> specificationMap = new HashMap<>();
                        specificationMap.put("value", detail.getSpecification());
                        detailMap.put("mx_gzxh", specificationMap);

                        Map<String, Object> specificationNewMap = new HashMap<>();
                        specificationNewMap.put("value", detail.getSpecificationNew());
                        detailMap.put("mx_cpggx", specificationNewMap);

                        Map<String, Object> productDescriptionMap = new HashMap<>();
                        productDescriptionMap.put("value", detail.getProductDescription());
                        detailMap.put("mx_cpms", productDescriptionMap);

                        Map<String, Object> additionalInfoNewMap = new HashMap<>();
                        additionalInfoNewMap.put("value", detail.getAdditionalInfoNew());
                        detailMap.put("mx_bcxxx", additionalInfoNewMap);

                        Map<String, Object> additionalInfoMap = new HashMap<>();
                        additionalInfoMap.put("value", detail.getAdditionalInfo());
                        detailMap.put("mx_bcxx", additionalInfoMap);

                        Map<String, Object> purchaserMap = new HashMap<>();
                        purchaserMap.put("value", detail.getPurchaser());
                        detailMap.put("mx_cgy", purchaserMap);

                        Map<String, Object> quantityMap = new HashMap<>();
                        quantityMap.put("value", detail.getQuantity());
                        detailMap.put("mx_sl", quantityMap);

                        Map<String, Object> unitNameMap = new HashMap<>();
                        unitNameMap.put("value", detail.getUnitName());
                        detailMap.put("mx_dj", unitNameMap);

                        Map<String, Object> baseUnitPriceMap = new HashMap<>();
                        baseUnitPriceMap.put("value", detail.getBaseUnitPrice());
                        detailMap.put("mx_bbdj", baseUnitPriceMap);

                        Map<String, Object> taxUnitPriceMap = new HashMap<>();
                        taxUnitPriceMap.put("value", detail.getTaxUnitPrice());
                        detailMap.put("mx_hsdj", taxUnitPriceMap);

                        Map<String, Object> taxRateMap = new HashMap<>();
                        taxRateMap.put("value", detail.getTaxRate());
                        detailMap.put("mx_slv", taxRateMap);

                        Map<String, Object> baseTaxAmountMap = new HashMap<>();
                        baseTaxAmountMap.put("value", detail.getBaseTaxAmount());
                        detailMap.put("mx_bbhsje", baseTaxAmountMap);

                        Map<String, Object> taxAmountMap = new HashMap<>();
                        taxAmountMap.put("value", detail.getTaxAmount());
                        detailMap.put("mx_hsje", taxAmountMap);

                        Map<String, Object> supplierCodeMap = new HashMap<>();
                        supplierCodeMap.put("value", detail.getSupplierCode());
                        detailMap.put("mx_gfbm", supplierCodeMap);


                        Map<String, Object> customsProductNameMap = new HashMap<>();
                        customsProductNameMap.put("value", detail.getCustomsProductName());
                        detailMap.put("mx_bgpm", customsProductNameMap);

                        Map<String, Object> productRequirementsMap = new HashMap<>();
                        productRequirementsMap.put("value", detail.getProductRequirements());
                        detailMap.put("mx_cpyq", productRequirementsMap);

                        Map<String, Object> packagingRequirementsMap = new HashMap<>();
                        packagingRequirementsMap.put("value", detail.getPackagingRequirements());
                        detailMap.put("mx_bzyq", packagingRequirementsMap);

                        Map<String, Object> isGiftMap = new HashMap<>();
                        isGiftMap.put("value", detail.getIsGift());
                        detailMap.put("mx_zp", isGiftMap);

                        Map<String, Object> isLineTerminatedMap = new HashMap<>();
                        isLineTerminatedMap.put("value", detail.getIsLineTerminated());
                        detailMap.put("mx_zzh", isLineTerminatedMap);

                        Map<String, Object> salesRepresentativeMap = new HashMap<>();
                        salesRepresentativeMap.put("value", detail.getSalesRepresentative());
                        detailMap.put("mx_xsywy", salesRepresentativeMap);

                        Map<String, Object> productRequirementsNewMap = new HashMap<>();
                        productRequirementsNewMap.put("value", detail.getProductRequirementsNew());
                        detailMap.put("mx_cpmsx", productRequirementsNewMap);

                        Map<String, Object> customerNameMap = new HashMap<>();
                        customerNameMap.put("value", detail.getCustomerName());
                        detailMap.put("mx_khmc", customerNameMap);

                        Map<String, Object> salesOrderCodeMap = new HashMap<>();
                        salesOrderCodeMap.put("value", detail.getSalesOrderCode());
                        detailMap.put("mx_xsddh", salesOrderCodeMap);

                        Map<String, Object> purchaseOrderCode = new HashMap<>();
                        purchaseOrderCode.put("value", detail.getPurchaseOrderCode());
                        detailMap.put("mx_cgddh", purchaseOrderCode);

                        result.add(detailMap);
                    }

                    Map<String, Object> mx = new HashMap<>();
                    mx.put("value", result);
                    data.put("mx", mx);
                    parameterInster.put("data", data);
                    String jsonSale = JSONUtil.toJsonStr(parameterInster);
                    ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSale);
                    i++;
                }
            }
        }
    }

    // 判断两个PurchaseOrder对象是否相等的方法
    private Map<String, Object> arePurchaseOrdersEqual(PurchaseOrder order, PurchaseOrder order1) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        boolean fale = true;
        // 逐个比较属性，如果任何一个属性不相等
        if (!Objects.equals(order.getAcceptDate(), order1.getAcceptDate())) {
            fale = false;
        }
        if (!Objects.equals(order.getName(), order1.getName())) {
            fale = false;
        }
        if (!Objects.equals(order.getVoucherdate(), order1.getVoucherdate())) {
            fale = false;
        }
        if (!Objects.equals(order.getSaleOrderCode(), order1.getSaleOrderCode())) {
            fale = false;
        }
        if (order.getOrigEarnestMoney() != order1.getOrigEarnestMoney()) {
            fale = false;
        }
        if (!Objects.equals(order.getPubuserdefnvc5(), order1.getPubuserdefnvc5())) {
            fale = false;
        }
        if (!Objects.equals(order.getPubuserdefnvc2(), order1.getPubuserdefnvc2())) {
            fale = false;
        }
        if (!Objects.equals(order.getPubuserdefnvc7(), order1.getPubuserdefnvc7())) {
            fale = false;
        }
        if (!Objects.equals(order.getCode(), order1.getCode())) {
            fale = false;
        }
        if (!Objects.equals(order.getDepartmentName(), order1.getDepartmentName())) {
            fale = false;
        }
        if (!Objects.equals(order.getPartnerAbbName(), order1.getPartnerAbbName())) {
            fale = false;
        }
        if (!Objects.equals(order.getPartnerName(), order1.getPartnerName())) {
            fale = false;
        }
        if (!Objects.equals(order.getPartnerCode(), order1.getPartnerCode())) {
            fale = false;
        }
        if (order.getTaxRate() != order1.getTaxRate()) {
            fale = false;
        }
        if (!Objects.equals(order.getPurchaserName(), order1.getPurchaserName())) {
            fale = false;
        }
        if (!Objects.equals(order.getPurchaserAddress(), order1.getPurchaserAddress())) {
            fale = false;
        }
        if (!Objects.equals(order.getMaker(), order1.getMaker())) {
            fale = false;
        }
        if (!Objects.equals(order.getMakerAddress(), order1.getMakerAddress())) {
            fale = false;
        }
        if (!fale) {
        }

        if (order.getPurchaseOrderDetails().size() != order1.getPurchaseOrderDetails().size()) {
            fale = false;
        }

        if (order.getPurchaseOrderDetails().size() == order1.getPurchaseOrderDetails().size()) {
            for (int i = 0; i < order.getPurchaseOrderDetails().size(); i++) {
                PurchaseOrderDetail detail1 = order.getPurchaseOrderDetails().get(i);
                PurchaseOrderDetail detail2 = order1.getPurchaseOrderDetails().get(i);
                if (!Objects.equals(detail1.getInventoryName(), detail2.getInventoryName())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getProductCode(), detail2.getProductCode())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getProductNameNew(), detail2.getProductNameNew())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getSpecification(), detail2.getSpecification())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getSpecificationNew(), detail2.getSpecificationNew())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getProductDescription(), detail2.getProductDescription())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getAdditionalInfoNew(), detail2.getAdditionalInfoNew())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getAdditionalInfo(), detail2.getAdditionalInfo())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getPurchaser(), detail2.getPurchaser())) {
                    fale = false;
                }
                if (detail1.getQuantity() != detail2.getQuantity()) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getUnitName(), detail2.getUnitName())) {
                    fale = false;
                }
                if (detail1.getBaseUnitPrice() != detail2.getBaseUnitPrice()) {
                    fale = false;
                }
                if (detail1.getTaxUnitPrice() != detail2.getTaxUnitPrice()) {
                    fale = false;
                }
                if (detail1.getTaxRate() != detail2.getTaxRate()) {
                    fale = false;
                }
                if (detail1.getBaseTaxAmount() != detail2.getBaseTaxAmount()) {
                    fale = false;
                }
                if (detail1.getTaxAmount() != detail2.getTaxAmount()) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getSupplierCode(), detail2.getSupplierCode())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getCustomsProductName(), detail2.getCustomsProductName())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getProductRequirements(), detail2.getProductRequirements())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getPackagingRequirements(), detail2.getPackagingRequirements())) {
                    fale = false;
                }
                if (detail1.getIsGift() != detail2.getIsGift()) {
                    fale = false;
                }
                if (detail1.getIsLineTerminated() != detail2.getIsLineTerminated()) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getSalesRepresentative(), detail2.getSalesRepresentative())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getProductRequirementsNew(), detail2.getProductRequirementsNew())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getCustomerName(), detail2.getCustomerName())) {
                    fale = false;
                }
                if (!Objects.equals(detail1.getSalesOrderCode(), detail2.getSalesOrderCode())) {
                    fale = false;
                }

            }
        }
        resultMap.put("data", order.getPurchaseOrderId());
        resultMap.put("code", fale);


        return resultMap;
    }
}
